import Vue from "vue";
// 通用跳转方法
// let routes = [...ROUTES]
let status = false
let history = ''
let timer = null
Vue.prototype.$navigateTo = function(url) {
  return new Promise (function (resolve, reject){
    if (status || history == url) return reject()
    if (url) {
      status = true
      uni.navigateTo({
        url,
        success: (res) => {
          status = false
          history = url
          if (timer) clearTimeout(timer)
          resolve(res)
          timer = setTimeout(()=>{
            history = ''
          },1000)
        },
        fail: (err) => {
          setTimeout(() => {
            uni.switchTab({
              url,
              success: (res) => {
                history = url
                if (timer) clearTimeout(timer)
                resolve(res)
                timer = setTimeout(()=>{
                  history = ''
                },1000)
              },
              fail: (err) => {
                reject()
                uni.showToast({
                  icon: 'none',
                  title: '敬请期待.'
                });
              },
              complete: () => {
                status = false
              }
            })
          })
        }
      });
      return;
    }
    reject()
    uni.showToast({
      icon: 'none',
      title: '敬请期待'
    });
  })
}
Vue.prototype.$redirectTo = function(url) {
  return new Promise (function (resolve, reject){
    if (status || history == url) return reject()
    if (url) {
      status = true
      uni.redirectTo({
        url,
        success: (res) =>{
          history = url
          if (timer) clearTimeout(timer)
          resolve(res)
          timer = setTimeout(()=>{
            history = ''
          },1000)
        },
        fail: (err) => {
          setTimeout(() => {
            uni.switchTab({
              url,
              success: (res) => {
                history = url
                if (timer) clearTimeout(timer)
                resolve(res)
                timer = setTimeout(()=>{
                  history = ''
                },1000)
              },
              fail: (err) => {
                reject()
                uni.showToast({
                  icon: 'none',
                  title: '敬请期待.'
                });
              },
              complete: () => {
                status = false
              }
            })
          })
        },
        complete: (err) => {
          status = false
        }
      });
      return
    }
    reject()
    uni.showToast({
      icon: 'none',
      title: '敬请期待'
    });
  })
}
uni.$navigateTo = Vue.prototype.$navigateTo
uni.$redirectTo = Vue.prototype.$redirectTo